home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / The Weakest Link / source / DNS code / CDNSAction.h < prev    next >
Encoding:
Text File  |  2001-06-23  |  4.3 KB  |  126 lines

  1. // =================================================================================
  2. //    CDNSAction.h         ©1999 Sustainable Softworks. All rights reserved.
  3. // =================================================================================
  4. //    Perform DNS Deferral
  5. //
  6. //    IPNetRouter does DNS forwarding using the NAT feature when there
  7. //    is no PPP interface or PPP is connected.  The NAT module is
  8. //  programmed to translate DNS requests to our local address to
  9. //  the currently configured DNS server, and the IP module forwards
  10. //    them.
  11. //
  12. //    When PPP is disconnected, the DNS NAT mappings are disabled so
  13. //  this module can receive DNS traffic and perform DNS Deferral.
  14. //    We intercept simple DNS lookup requests and generate an
  15. //    intermediate response at 2 second intervals until PPP comes
  16. //    back on-line (or we time out).  The purpose of these intermediate
  17. //    responses is to prevent the clients DNR from timing out while
  18. //  waiting for PPP to connect.
  19. //
  20. //    We never actually forward a DNS request on behalf of a client,
  21. //    rather, we string the client along with one CNAME after another
  22. //    until the DNS port mappings are back online, then we tell the
  23. //    client to retry the original DNS request, but this time the request
  24. //  is redirected to a real Name Server by the NAT module.
  25.  
  26. #pragma once
  27.  
  28. #include "myDNS.h"    // pick up protocol definitions
  29.  
  30. #include <LCleanupTask.h>
  31. #include <LPeriodical.h>
  32. #include <LListener.h>
  33. #include "CObjectMaster.h"
  34.  
  35. #define kMaxServerDim    4
  36.  
  37. // Generic message descriptor
  38. struct msg_descriptor {
  39.     UInt8*    data;    // pointer to buffer containing message
  40.     UInt32    size;    // size of message in buffer
  41.     UInt16    offset;    // offset to next item in message
  42. };
  43. typedef struct msg_descriptor msg_descriptor_t;
  44.  
  45. #define kMaxResponseLen    1460
  46. // DNS request (collect DNS message information)
  47. struct dns_request {
  48.     msg_descriptor_t md;
  49.     UInt16    remotePort;
  50.     UInt32    remoteAddr;
  51.     OTTimeStamp    timeStamp;    // time when received
  52.     // data buffer
  53.     UInt8    buf[kMaxResponseLen];
  54. };
  55. typedef struct dns_request dns_request_t;
  56.  
  57. // when to check timeouts
  58. const SInt16 kDNSIdleDefault        = 5000;    // test every 5 seconds
  59. const SInt16 kDNSIdleRestart        = 2000;    // restart after 2 second
  60. const SInt16 kDNSIdleData            = 200;    // data waiting, every 250 ms
  61. const SInt16 kDNSRetryCount            = 10;    // number times to try restarting
  62.  
  63. // dns deferral strings
  64. const Str31 kDialingPrefixStr        = "\p.dialing-please-wait-";
  65. const Str31 kDialingSufixStr        = "\p.apple.com";
  66.  
  67.  
  68. class CTurboUDPEndpoint;
  69. class CReceiveUDPThread;
  70. class CSendUDPThread;
  71. class LArray;
  72. class LComparator;
  73. class LFile;
  74.  
  75. class CDNSAction :    public LPeriodical, LListener, CObjectMaster {
  76. public:
  77.                                 CDNSAction();
  78.     virtual                         ~CDNSAction();
  79.     void                Terminate(Boolean inCanWait=true);
  80.     Boolean                StartServing();
  81.     Boolean                StopServing(Boolean inCanWait=true);
  82.     void                StopDeferral();
  83.     void                ResumeDeferral();
  84.     virtual void        SpendTime(const EventRecord &inMacEvent);
  85.  
  86.     virtual void        ObjectThreadDied(LThread *inThread);
  87.     void                ListenToMessage(
  88.                             MessageT    inEventCode,
  89.                             void        *ioParam );
  90.     void                ReceiveData(LDataArrived* inMessage);
  91.  
  92. protected:
  93.     void                ProcessRequest(dns_request_t* inRequest);
  94.     Boolean                SendResponse(dns_request_t* inRequest);
  95.     OTResult            ForwardToDNS(UInt8* inData, UInt32 inDataSize);
  96.     void                ReceiveDNRProxy(LDataArrived* inMessage);
  97.     OTResult            ForwardFromDNS(UInt8* inData, UInt32 inDataSize);
  98.     Boolean                GetDName(msg_descriptor_t* md, Str255 outStr);
  99.     Boolean                PutDName(msg_descriptor_t* md, ConstStr255Param inStr);
  100.     UInt8                IsFakeDialingName(ConstStr255Param inStr);
  101.     long                GetFakeDialingName(ConstStr255Param inStr, Str255 outStr);
  102.     StringPtr            GetRealDialingName(ConstStr255Param inStr, Str255 outStr);
  103.     void                LastComplete();
  104.  
  105.     LArray*                mResponseArray;
  106.     Boolean                mIsServing;
  107.     Boolean                mWaitingToStop;
  108.     Boolean                mWaitingToRestart;
  109.     UInt8                mDNSRetryCount;
  110.     
  111. private:    
  112.     // UDP query
  113.     UInt32                mServerAddr[kMaxServerDim];
  114.     CTurboUDPEndpoint*    mUDPEndpoint;                // our UDP network endpoint object
  115.     CReceiveUDPThread*    mReceiveUDPThread;
  116.     CSendUDPThread*        mSendUDPThread;
  117.     OTTimeStamp            mLastStamp;
  118.     OTTimeStamp            mStopStamp;
  119.     UInt32                mIdleDelay;                    // milliseconds
  120.     // Local DNS forwarding
  121.     CTurboUDPEndpoint*    mDNRProxyEndpoint;
  122.     CReceiveUDPThread*    mDNRProxyRxThread;
  123.     UInt32                mDNRSourceAddr;
  124.     UInt16                mDNRSourcePort;
  125. };
  126.